package com.jinsilu.freight.ui.activity;

import android.text.Html;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.BusUtils;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.jinsilu.freight.R;
import com.jinsilu.freight.aop.SingleClick;
import com.jinsilu.freight.common.MyVBActivity;
import com.jinsilu.freight.databinding.ActivityUploadRoadTransportBinding;
import com.jinsilu.freight.helper.ShowImageHelper;
import com.jinsilu.freight.http.model.HttpData;
import com.jinsilu.freight.http.request.GetRoadTransportApi;
import com.jinsilu.freight.http.request.SubmitCertificateApi;
import com.jinsilu.freight.http.request.UpdateImageApi;
import com.jinsilu.freight.http.request.VerifyRoadTransportApi;
import com.jinsilu.freight.http.response.FileBean;
import com.jinsilu.freight.http.response.RoadTransportBean;
import com.jinsilu.freight.other.MTLConstants;

import java.io.File;

/**
 * 上传道路运输证
 *
 * @author yangyang
 * @time 2020-11-11 17:35
 */
public class UploadRoadTransportActivity extends MyVBActivity<ActivityUploadRoadTransportBinding> {

    private final String TAG = getClass().getName();

    private String picUrl;

    private RoadTransportBean mRoadTransportBean;


    @Override
    protected void initView() {
//        binding.tvNotificationTips.setVisibility(View.VISIBLE);
//        binding.tvNotificationTips.setText("拍摄图片时，请保持画面文字的清晰。");

        setOnClickListener(binding.ivRoadTransportPhoto, binding.tvBtnSubmitCertificate);

    }

    @Override
    protected void initData() {
        getRoadTransportStatus();
        initStep();
    }

    private void initStep() {
        binding.layoutCertification.tvCertificationTips.setText("请使用本人身份信息进行认证，若使用他人身份认证并接单，将造成运费无法提现");
        binding.layoutCertification.tvCertificationTips.setVisibility(View.GONE);
        binding.tvCheckTheInformation.setText(Html.fromHtml(getString(R.string.tips_check_the_information)));
        binding.layoutCertification.stepView.go(MTLConstants.STEP_FIVE, true);
    }

    /**
     * 获取道路运输证状态
     */
    private void getRoadTransportStatus() {
        EasyHttp.get(this)
                .api(new GetRoadTransportApi())
                .request(new HttpCallback<HttpData<RoadTransportBean>>(this) {

                    @Override
                    public void onSucceed(HttpData<RoadTransportBean> data) {
                        mRoadTransportBean = data.getData();
                        setCertificateStatus(mRoadTransportBean);
                    }
                });
    }

    private void setCertificateStatus(RoadTransportBean roadTransportBean) {
        switch (roadTransportBean.getVehicleTransportLicenseStatus()) {
            case -2: // 未认证
                break;
            case 0: // 认证失败
//                binding.layoutCertification.ivCertificationResult.setImageResource(R.drawable.approved_rejected_ic);
//                binding.layoutCertification.ivCertificationResult.setVisibility(View.VISIBLE);
//                binding.layoutCertification.tvCertificationStatus.setText("审核未通过");
//                binding.layoutCertification.tvCertificationStatus.setVisibility(View.VISIBLE);
                binding.layoutCertification.tvCertificationTips.setText(TextUtils.isEmpty(roadTransportBean.getOpinion()) ? "" : roadTransportBean.getOpinion());
                binding.layoutCertification.tvCertificationTips.setVisibility(View.VISIBLE);
                setContentData(roadTransportBean);
                break;
            case 1: // 认证成功
//                binding.layoutCertification.ivCertificationResult.setImageResource(R.drawable.approved_ic);
//                binding.layoutCertification.ivCertificationResult.setVisibility(View.VISIBLE);
//                binding.layoutCertification.tvCertificationStatus.setText("审核通过");
//                binding.layoutCertification.tvCertificationStatus.setVisibility(View.VISIBLE);
                disableClicked(getString(R.string.tips_approved));
                setContentData(roadTransportBean);
                break;
            case -1: // 提交全部认证资料 认证中
//                binding.layoutCertification.ivCertificationResult.setImageResource(R.drawable.approving_ic);
//                binding.layoutCertification.ivCertificationResult.setVisibility(View.VISIBLE);
//                binding.layoutCertification.tvCertificationStatus.setText("审核中");
//                binding.layoutCertification.tvCertificationStatus.setVisibility(View.VISIBLE);
                disableClicked(getString(R.string.tips_approving));
                setContentData(roadTransportBean);
                break;
            default:
                break;
        }
    }

    /**
     * 设置不允许点击
     */
    private void disableClicked(String tips) {
        binding.ivRoadTransportPhoto.setEnabled(false);
        binding.etRoadTransportNumber.setEnabled(false);
        binding.tvBtnSubmitCertificate.setText(getString(R.string.common_confirm
        ));

        binding.layoutCertification.stepView.done(true);

        binding.layoutCertification.tvCompleteCanTakeOrder.setText(tips);
        binding.layoutCertification.tvCertificationTips.setVisibility(View.GONE);

        binding.tvRoadTransportTips.setVisibility(View.GONE);

    }

    /**
     * 数据回填
     *
     * @param roadTransportBean 身份数据
     */
    private void setContentData(RoadTransportBean roadTransportBean) {
        picUrl = roadTransportBean.getVehicleTransportLicense();
        ShowImageHelper.showNormal(this, roadTransportBean.getVehicleTransportLicense(), binding.ivRoadTransportPhoto);
        binding.etRoadTransportNumber.setText(roadTransportBean.getVehicleTransportLicenseNumber());
        binding.tvBtnSubmitCertificate.setEnabled(true);
    }

    @SingleClick
    @Override
    public void onClick(View v) {
        if (v == binding.ivRoadTransportPhoto) {
            startImageSelectActivity(0);
        } else if (v == binding.tvBtnSubmitCertificate) {
            if (null == mRoadTransportBean) {
                toast(R.string.error_certification_failed_with_network);
                return;
            }
            switch (mRoadTransportBean.getVehicleTransportLicenseStatus()) {
                // -2---未认证，-1---待审核，0---失败，1---成功
                case -2: // 未认证
                case 0: // 认证失败
                    submitInfo();
                    break;
                case -1: // -待审核
                case 1: // 认证成功
                    closeAllSteps();
                    break;
                default:
                    break;
            }
        }
    }

    /**
     * 关闭认证
     */
    private void closeAllSteps() {
        ActivityUtils.finishActivity(UploadIdCardActivity.class);
        ActivityUtils.finishActivity(UploadDrivingLicenseActivity.class);
        ActivityUtils.finishActivity(UploadQualificationCertificateActivity.class);
        ActivityUtils.finishActivity(UploadVehicleLicenseActivity.class);
        finish();
    }

    /**
     * 身份验证
     */
    private void submitInfo() {
        if (TextUtils.isEmpty(picUrl)) {
            toast("请上传道路运输证");
            return;
        }
        String qualificationNumber = binding.etRoadTransportNumber.getText().toString().trim();
        if (TextUtils.isEmpty(qualificationNumber)) {
            toast("请输入道路运输证号");
            return;
        }

        EasyHttp.post(this)
                .api(new VerifyRoadTransportApi().setVehicleTransportLicense(picUrl).setVehicleTransportLicenseNumber(qualificationNumber))
                .request(new HttpCallback<HttpData<Void>>(this) {

                    @Override
                    public void onSucceed(HttpData<Void> data) {
//                        setResult(RESULT_OK);
//                        finish();
                        completeSubmit();

                    }

                });
    }

    /**
     * 认证信息总提交
     */
    private void completeSubmit() {
        EasyHttp.put(this)
                .api(new SubmitCertificateApi())
                .request(new HttpCallback<HttpData<Void>>(this) {

                    @Override
                    public void onSucceed(HttpData<Void> data) {
                        toast("认证信息已提交，请耐心等待审核结果");
                        BusUtils.postSticky(HomeActivity.TAG_REFRESH_USER_DATA); // 刷新用户信息
                        closeAllSteps();
                    }
                });
    }

    /**
     * 启动图片选择
     */
    private void startImageSelectActivity(int index) {
        ImageSelectVBActivity.start(this, new ImageSelectVBActivity.OnPhotoSelectListener() {

            @Override
            public void onSelected(File file) {
//                File file = CommonHelper.getFileFromUri(data);
                uploadImage(file);
            }

            @Override
            public void onCancel() {
            }
        });
    }

    private void uploadImage(File file) {
        // 上传文件
        EasyHttp.post(this)
                .api(new UpdateImageApi()
                        .setImage(file))
                .request(new HttpCallback<HttpData<FileBean>>(this) {

                    @Override
                    public void onSucceed(HttpData<FileBean> data) {
//                        FileUtils.delete(file);
                        String imgUrl = data.getData().getUrl();
                        picUrl = imgUrl;
                        ShowImageHelper.showNormal(UploadRoadTransportActivity.this, imgUrl, binding.ivRoadTransportPhoto);
                    }
                });
    }
}
